﻿var circles = [];
var depmap = {};

function show(map,year){
	if(circles.length!=0 ){
		for(var i=0;i<circles.length;i++){
			circles[i].setMap(null);
		}
	}
	circles = [];
	var max=0;
	var max_dep="";
	var min=10000000000;
	var min_dep="";
	
	for(var i=1;i<departments.length;i++){
		depmap[departments[i]] = {
			center: new google.maps.LatLng(depCoordinates[departments[i]].longitude ,depCoordinates[departments[i]].latitude) ,
			delinquency: data[year][departments[i]]
		}
		if(max < data[year][departments[i]]){
			max = data[year][departments[i]];
			max_dep = departments[i];
		}
		if(min > data[year][departments[i]]){
			min=data[year][departments[i]];
			min_dep = departments[i];
		}
	}

	for (var dep in depmap){
		var color="orange";
		if(dep==max_dep){
			color="red";
		}
		if(dep==min_dep){
			color="#7FFF00";
		}
		var delinquencyOptions = {
			strokeColor:color ,
			strokeOpadep: 0.9,
			strokeWeight: 2,
			fillColor: color,
			fillOpadep: 0.6,
			map: map,
			center: depmap[dep].center,
			title:dep,
			radius: depmap[dep].delinquency/25,
			animation: google.maps.Animation.DROP
		};
	

		depCircle = new google.maps.Circle(delinquencyOptions);
		circles.push(depCircle);
		google.maps.event.addListener(depCircle, 'click', function(event){
	
		if($('#detail').children().length > 0){
			$('#detail').empty();
		}
		var contentString = '<div id="contentTooltip"><p><i>Departement:</i><b> '+this.title+'</b></br><i>Délinquance:</i><span> '+(depmap[this.title].delinquency)+'</span></p></div>';
		$("#detail").append(contentString);
		});
		
	}
}

function initialize(){
				var mapOptions = {
					zoom: 4,
					center: new google.maps.LatLng(46.845164,2.570801),
					mapTypeId: google.maps.MapTypeId.TERRAIN
				};
				map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
				show(map,"2002");
			}

var map;
var ContentTab="";
var exist=false;
 $('#map').live('pageshow', function(event) { 
	$("#select-choice-1").bind( "change", function(event, ui) {
		var $value = $(this).children("option:selected").text();
		$('#detail').empty();
		show(map,$value);
		
	});
    initialize();  
		
    });  
